"""
    合同数据清洗
"""
from typing import Dict
from core.config import settings


def clean_data(contract_data: Dict) -> Dict:
    """
    对传入的数据进行清洗和修正
    """
    index = 0
    text_blocks = []
    table_block = {}
    table_contents = []
    table_words = []
    for block in contract_data['textBlocks']:
        # 表格数据处理
        if block['blockType'] == 'table':
            if len(table_contents) > 1:
                table_block['content'] = '\n'.join(table_contents)
                table_block['words'] = '\n'.join(table_words)
                table_block['blockIndex'] = index
                text_blocks.append(table_block)
                index += 1
            table_block = block
            table_block.pop('tableRowIndex', None)
        elif block['blockType'] == 'row':
            if len(table_block) != 0 and block.get('parentId', '') == table_block['id']:
                all_words = '\n'.join(table_words) + '\n' + str(block['words'])
                if len(all_words) > settings.CHUNK_SIZE:
                    table_block['content'] = '\n'.join(table_contents)
                    table_block['words'] = '\n'.join(table_words)
                    table_block['blockIndex'] = index
                    text_blocks.append(table_block)
                    index += 1
                    table_contents = table_contents[0:1]
                    table_words = table_words[0:1]
                table_contents.append(str(block['content']))
                table_words.append(str(block['words']))
        elif block['blockType'] == 'text':
            if len(table_contents) > 1:
                table_block['content'] = '\n'.join(table_contents)
                table_block['words'] = '\n'.join(table_words)
                table_block['blockIndex'] = index
                text_blocks.append(table_block)
                index += 1
                table_block = {}
                table_contents = []
                table_words = []

            content = str(block['content']).strip()
            if content == '':
                continue
            block['content'] = content
            block['blockIndex'] = index
            text_blocks.append(block)
            index += 1

    fixed_data = {
        'fileId': contract_data['fileId'],
        'documentName': contract_data['documentName'],
        'textBlocks': text_blocks,
    }

    return fixed_data